package code08_常用类.String;

import java.util.Arrays;

/**
 * @author 黄衡熙
 * @version 1.0
 */
public class Test02_常用方法 {
    public static void main(String[] args) {
        String s = " Hello World Hello admin Hello jack Hello tom Hello lucy ";
        System.out.println("字符串长度:" + s.length());
        // 去除首尾的空格,可以将取出首尾空格后的字符串赋值给指定变量
        s = s.trim();
        System.out.println("字符串长度:" + s.length());

        System.out.println("子字符串Hello在当前字符串中第一次出现的索引位置:" + s.indexOf("Hello"));
        System.out.println("子字符串Hello在当前字符串中索引位置5之后第一次出现的索引位置:" + s.indexOf("Hello", 5));
        System.out.println("子字符串Hello在当前字符串中最后一次出现的索引位置:" + s.lastIndexOf("Hello"));
        System.out.println("子字符串Hello在当前字符串中截止到索引40的位置时最后一次出现的索引位置:" + s.lastIndexOf("Hello", 40));

        System.out.println("截取从索引20开始到末尾:" + s.substring(20));
        System.out.println("截取从索引20开始到索引30:" + s.substring(20, 31));

        // 字符串拼接
        String s1 = " Hello john";
        String s2 = " Hello alice";
        System.out.println(s.concat(s1).concat(s2));

        System.out.println("字符串s是否为空:" + s.isEmpty());
        String s3 = "";
        System.out.println("字符串s3是否为空:" + s3.isEmpty());
        System.out.println("获取索引位置20的字符:" + s.charAt(20));
        System.out.println("获取索引位置30的字符:" + s.charAt(30));

        System.out.println(s);
        System.out.println("字符串s是否以Hello开头:" + s.startsWith("Hello"));
        System.out.println("字符串s是否以Hello结尾:" + s.endsWith("Hello"));
        System.out.println("字符串s是否以lucy结尾:" + s.endsWith("lucy"));

        System.out.println("-------------------------------------");
        // 区分大小写
        System.out.println("字符串s中是否包含指定子字符串Hello:" + s.contains("Hello"));
        System.out.println("字符串s中是否包含指定子字符串hello:" + s.contains("hello"));

        String s4 = new String("admin");
        String s5 = new String("admin");
        System.out.println(s4 == s5);
        System.out.println(s4.equals(s5));
        String s6 = new String("ADMIN");
        // 区分大小写
        System.out.println(s4.equals(s6));
        // 不区分大小写
        System.out.println(s4.equalsIgnoreCase(s6));
        System.out.println("转大写:" + s.toUpperCase());
        System.out.println("转小写:" + s.toLowerCase());
        //替换
        System.out.println(s.replace(" ", "-"));
        //分割成数组
        String[] arr = s.split(" ");
        System.out.println(Arrays.toString(arr));
    }
}
